﻿using Business.Base.Model;
using Business.NPSH;
using Component;
using Pumptester.ExperimentForms.Base;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pumptester.ExperimentForms.Npsh
{
    public class NpshChart : IChartDrawer
    {
        public void DrawChart(BaseChartData chartData, UserChart chart)
        {
            NpshChartData chartDatainfo = (NpshChartData)chartData;
            Component.Axis xAxis = new Component.Axis();
            xAxis.Name = "汽蚀余量";
            xAxis.MaxValue = chartDatainfo.MaxXNAxis;
            xAxis.MinValue = chartDatainfo.MinXNAxis;
            xAxis.SegCount = chartDatainfo.XUnitCount;

            Component.Axis yAxis = new Component.Axis();
            yAxis.Name = "扬程";
            yAxis.MaxValue = chartDatainfo.MaxYHAxis;
            yAxis.MinValue = chartDatainfo.MinXNAxis;

            Coordinate coordinate = new Coordinate();
            coordinate.XAxis = xAxis;
            coordinate.YAxis = yAxis;
            coordinate.AxisDescritionBrush = new SolidBrush(Color.White);
            coordinate.AxisPen = new Pen(Color.White);
            coordinate.CurvePen = new Pen(new SolidBrush(Color.Blue), 2);
            coordinate.PointBrush = new SolidBrush(Color.Yellow);

        

            SolidBrush defaultAuxiliaryAxisPointBrush = new SolidBrush(Color.White);
            coordinate.AuxiliaryAxisPointBrush =  defaultAuxiliaryAxisPointBrush;
            coordinate.AxisDescritionBrush = new SolidBrush(Color.White);
            coordinate.AxisPen = new Pen(Color.White);

            coordinate.CurvePen = new Pen(new SolidBrush(Color.White), 1);
            coordinate.PointBrush = new SolidBrush(Color.Yellow);
            coordinate.LinePen = new Pen(new SolidBrush(Color.White), 1);
            coordinate.AuxiliaryBrush = coordinate.AxisDescritionBrush = new SolidBrush(Color.White);


            for (int i = 0; i < chartDatainfo.InputPoints.Count; i++)
            {

                double npsh = chartDatainfo.InputPoints[i][0];
                double head = chartDatainfo.InputPoints[i][1];
                coordinate.Points.Add(new Component.Point(npsh, head));
            }



            int pointCount = chartDatainfo.DrawList.Count;
            List<Component.Point> firstPoints = new List<Component.Point>();
      
            for (int i = 0; i < pointCount; i++)
            {
                double[] data = chartDatainfo.DrawList[i];
                double x = data[0];
      
                double[] hPoints = new double[] { x, data[1] };
              

                Component.Point p1 = new Component.Point(hPoints[0], hPoints[1]);
                firstPoints.Add(p1);
            }
            Curve firstCurve = new Curve(firstPoints);
            coordinate.Curves.Add(firstCurve);

            #region 画出必须汽蚀余量的点

            if (!Double.IsNaN(chartDatainfo.NPSHR))
            {
                coordinate.Points.Add(new Component.Point(chartDatainfo.NPSHR, chartDatainfo.PreRatedHead,Color.Blue));

                coordinate.Lines.Add(new
                    Line(new Component.Point(chartDatainfo.NPSHR, 0),
                         new Component.Point(chartDatainfo.NPSHR, chartDatainfo.RatedHead)
                         )
                         );
            }
            coordinate.Points.Add(new Component.Point(chartDatainfo.RatedNpsh, chartDatainfo.RatedHead, Color.Orange));
            coordinate.Lines.Add(new 
                Line(new Component.Point(0, chartDatainfo.PreRatedHead),
                     new Component.Point(chartDatainfo.MaxXNAxis, chartDatainfo.PreRatedHead)
                     )
                     );

            #endregion


            chart.FirstCoordinate = coordinate;
            chart.Refresh();

        }
    }
}
